package com.dbsop.domain.params;

import com.dbsop.common.util.EsFieldUtils;
import com.dbsop.common.util.EsPoJoUtils;
import com.dbsop.domain.info.EsFiledInfo;
import com.dbsop.domain.info.Params;
import com.dbsop.mapper.IndexMappingInfo;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Setter
@Getter
public class SaveParams<T> implements Params {

    private String index;

    private IndexMappingInfo indexInfo;

    private T t;

    private List<T> ts;

    private Class<T> rClass;

    private String idName;

    /**
     * false 保存为null的值  true 保存不为null的值
     */
    private boolean isNull = false;


    public SaveParams(IndexMappingInfo indexInfo, T t) {
        this.indexInfo = indexInfo;
        this.t = t;
    }

    public SaveParams(IndexMappingInfo indexInfo, T t, boolean isNull) {
        this.indexInfo = indexInfo;
        this.t = t;
        this.isNull = isNull;
    }

    public SaveParams(IndexMappingInfo indexInfo, List<T> ts, boolean isNull) {
        this.indexInfo = indexInfo;
        this.ts = ts;
        this.isNull = isNull;
    }

    public SaveParams(IndexMappingInfo indexInfo, List<T> ts) {
        this.indexInfo = indexInfo;
        this.ts = ts;
    }

    public SaveParams(String index, IndexMappingInfo indexInfo, T t) {
        this.index = index;
        this.indexInfo = indexInfo;
        this.t = t;
    }

    public SaveParams(String index, IndexMappingInfo indexInfo,List<T> ts) {
        this.index = index;
        this.indexInfo = indexInfo;
        this.t = t;
        this.ts = ts;
    }

    public Map<String,Object> getSourceMap() {
        Map<String, EsFiledInfo> filedInfoMap = null;
        if (rClass!=null){
            filedInfoMap =   EsFieldUtils.pareFieldEsInfo(rClass);
        }else {
            filedInfoMap = indexInfo.getFieldMap();
        }

        if (isNull){
            return EsPoJoUtils.getFieldValueMap(t, filedInfoMap);
        }else {
            return EsPoJoUtils.getFieldNotNullValueMap(t, filedInfoMap);
        }
    }

    public String getIdName(){
        if (idName == null){
            return indexInfo.getIdName();
        }
        return idName;
    }

    public  Map<String,List<Map<String,Object>>> getListSourceMap() {
       return indexInfo.sortingData(ts,isNull);
    }

    @Override
    public void indexHandler() {

    }

    @Override
    public String[] getIndex() {
        if (t !=null){
            return new String[]{index==null? indexInfo.getIndex(null,t): indexInfo.getIndex(index,t)};
        }
        return new String[]{indexInfo.getIndexName()};
    }
}
